 

options compress = yes obs = max;

libname tx "C:\DATA OFFLINE\Hospital Texas 01";
libname inter "C:\Data\Inter";


proc sort data = TX.ownership; by THCIC_ID; run;

%macro pull_TX(file);
data junk1 ;
   set TX.&file. (keep = pat_status pat_age admit_start_of_care birth_date PRINC_DIAG_CODE 
                          SEX_CODE source_of_admission THCIC_ID SOURCE_PAYMENT_CODE_1);
   format ad_date date.;
   file = "&file.";
   state = 'TX';
   byear = floor(birth_date/10000); 
   bmonth = floor((birth_date - byear*10000)/100);
   bday = birth_date - 10000*byear -100*bmonth;
   ayear = floor(admit_start_of_care/10000); 
   amonth = floor((admit_start_of_care - 10000*ayear)/100);
   aday = admit_start_of_care - 10000*ayear -100*amonth;
   months_23 = intck('month',mdy(bmonth,14,byear+23),mdy(amonth,15,ayear));
   ad_date = mdy(amonth,15,ayear);
   days_23 = intck('day',mdy(bmonth,bday,byear+23),mdy(amonth,aday,ayear));
   if months_23 = . then bad_date = 1; else bad_date = 0;
   if source_of_admission = "7" then from_ED = 1; else from_ED = 0;
   if pat_status = 20 then died = 1; else died = 0;
   if substr(PRINC_DIAG_CODE,1,2) in ('63','64','65','66','67') then preg_related = 1; else preg_related = 0;
   if (months_23 >= -40 and months_23 <= 40) or bad_date = 1;
   if SEX_CODE = 'M' then male = 1; 
   if SEX_CODE = 'F' then male = 0;
   count = 1;
run;
proc freq data = junk1;
   tables count;
   where  bad_date = 1 and pat_age = "07"; *07 is 20-24;
   title "&File. Missing Exact Age";
run;
proc sort data = junk1 out = junk2; by THCIC_ID; run;
data &file.;
   merge junk2 (in = ina) TX.ownership;
   by THCIC_ID;
   if ina = 1;
	if bad_date = 1 then delete;
	if state = 'TX' and ad_date < mdy(1,1,1999) then delete;
	visit = 1;
   if SOURCE_PAYMENT_CODE_1 = 'D' then medicaid = 1; else medicaid = 0;
   if SOURCE_PAYMENT_CODE_1 in ('F','G') then private = 1; else private = 0;
   if SOURCE_PAYMENT_CODE_1 = 'A' then self_pay = 1; else self_pay = 0;
   if SOURCE_PAYMENT_CODE_1 not in ('A','D','F','G') then other_ins = 1; else other_ins = 0;
   if Ownership_ = 'Public' then cntrl_public = 1; else cntrl_public = 0;
   if Ownership_ = 'Non Profit' then cntrl_non_profit = 1; else cntrl_non_profit = 0;
   if Ownership_ = 'For Profit' then cntrl_for_profit = 1; else cntrl_for_profit = 0;
   if Ownership_ not in ('Public','Non Profit','For Profit') then cntrl_other = 1; else cntrl_other = 0;
run;
proc datasets;
   delete junk1;
run;
%mend;
%pull_TX(Texas99);
%pull_TX(Texas00);
%pull_TX(Texas01);
%pull_TX(Texas02);
%pull_TX(Texas03);

proc freq data = tx.texas00;
   tables SOURCE_PAYMENT_CODE_1*payment_source_1;
run;

data TEXAS_inp; 
   format ad_date date.;
   set Texas99 Texas00 Texas01 Texas02 Texas03;
run;

%macro stab(i_file,o_file);
proc means data = &i_file. noprint;
   var visit male   preg_related from_ED medicaid private self_pay other_ins cntrl_public cntrl_non_profit cntrl_for_profit cntrl_other;
   output out = junk1 sum = visit male   preg_related from_ED medicaid private self_pay other_ins cntrl_public cntrl_non_profit cntrl_for_profit cntrl_other;
   class ad_date;
   where months_23 >= -12 and months_23 <=12;
run;
data junk2;
   format ad_date date.;
   set junk1;
   if _type_ = 0 then delete;
run; 
PROC EXPORT DATA= junk2
            OUTFILE= "C:\Research\Insurance 23\Reproduction from Scratch\&o_File..csv"
            DBMS=CSV REPLACE;
RUN;
proc datasets;
   delete junk1 junk2;
run;
%mend;

%stab(TEXAS_inp,P07 Texas Inpatient);

%macro pull_v(suf,a_where);
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk1 (rename=(_FREQ_ = &st._&suf.)) mean =  &st._medi_&suf. &st._priv_&suf. &st._self_&suf. &st._o_ins_&suf.;
   class months_23;
   where state = "&st." &a_where.;
run;
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk2 (rename=(_FREQ_ = &st._&suf._ED)) mean =  &st._medi_&suf._ED &st._priv_&suf._ED &st._self_&suf._ED &st._o_ins_&suf._ED;
   class months_23;
   where state = "&st." and from_ED = 1 &a_where.;
run;
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk3 (rename=(_FREQ_ = &st._&suf._NED)) mean =  &st._medi_&suf._NED &st._priv_&suf._NED &st._self_&suf._NED &st._o_ins_&suf._NED;
   class months_23;
   where state = "&st." and from_ED = 0 &a_where.;
run;
data &suf.;
   merge junk1 junk2 junk3;
   by months_23;
run;
%mend;

%macro p_state(st,i_File);
   %pull_v(all,);
   %pull_v(all_pub,and cntrl_public = 1);
   %pull_v(all_non_prof,and cntrl_non_profit = 1);
   %pull_v(all_for_prof,and cntrl_for_profit = 1);
   %pull_v(all_other,and cntrl_other = 1);
   %pull_v(all_np,and preg_related = 0);
   %pull_v(all_np_pub,and preg_related = 0 and cntrl_public = 1);
   %pull_v(all_np_non_prof,and preg_related = 0 and cntrl_non_profit = 1);
   %pull_v(all_np_for_prof,and preg_related = 0 and cntrl_for_profit = 1);
   %pull_v(all_np_other,and preg_related = 0 and cntrl_other = 1);
   %pull_v(female,and male = 0);
   %pull_v(female_pub,and male = 0 and cntrl_public = 1);
   %pull_v(female_non_prof,and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_for_prof,and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_other,and male = 0 and cntrl_other = 1);
   %pull_v(female_np,and preg_related = 0 and male = 0);
   %pull_v(female_np_pub,and preg_related = 0 and male = 0 and cntrl_public = 1);
   %pull_v(female_np_non_prof,and preg_related = 0 and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_np_for_prof,and preg_related = 0 and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_np_other,and preg_related = 0 and male = 0 and cntrl_other = 1);
   %pull_v(female_p,and preg_related = 1 and male = 0);
   %pull_v(female_p_pub,and preg_related = 1 and male = 0 and cntrl_public = 1);
   %pull_v(female_p_non_prof,and preg_related = 1 and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_p_for_prof,and preg_related = 1 and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_p_other,and preg_related = 1 and male = 0 and cntrl_other = 1);
   %pull_v(male,and male = 1);
   %pull_v(male_pub,and male = 1 and cntrl_public = 1);
   %pull_v(male_non_prof,and male = 1 and cntrl_non_profit = 1);
   %pull_v(male_for_prof,and male = 1 and cntrl_for_profit = 1);
   %pull_v(male_other,and male = 1 and cntrl_other = 1);
   data inter.ins_23_reprod_&ST. (drop = _type_ );
      merge all 		all_pub 		all_non_prof 		all_for_prof 		all_other
       	 	all_np 		all_np_pub 		all_np_non_prof 	all_np_for_prof 	all_np_other
         	female 		female_pub 		female_non_prof 	female_for_prof 	female_other
   		 	female_np 	female_np_pub 	female_np_non_prof 	female_np_for_prof 	female_np_other
   		 	female_p 	female_p_pub 	female_p_non_prof 	female_p_for_prof 	female_p_other
   		 	male 		male_pub 		male_non_prof 		male_for_prof 		male_other;
      by months_23;
      if months_23 = . then delete;
      merge_v = 1;
   run;
%mend;

%p_state(TX,Texas_inp);


